package cloud.maque.module.system.service.impl;

import cloud.maque.common.core.utils.TokenUserUtil;
import cloud.maque.module.system.entity.SysMenuPrivilege;
import cloud.maque.module.system.entity.SysPrivilege;
import cloud.maque.module.system.mapper.MenuPrivilegeMapper;
import cloud.maque.module.system.service.MenuPrivilegeService;
import cloud.maque.module.system.service.PrivilegeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MenuPrivilegeServiceImpl extends ServiceImpl<MenuPrivilegeMapper, SysMenuPrivilege> implements MenuPrivilegeService {


    @Autowired
    PrivilegeService privilegeService;
    @Autowired
    TokenUserUtil userUtil;

    @Override
    public List<SysMenuPrivilege> GetList(SysMenuPrivilege data) {
        QueryWrapper<SysMenuPrivilege> query = new QueryWrapper<>();
        query.lambda().eq(SysMenuPrivilege::getMenuId, data.getMenuId());
        return this.list(query);
    }

    @Override
    public void SavePrivilege(String menuId) {

        //先删除，再新增
        this.remove(new QueryWrapper<SysMenuPrivilege>().lambda().eq(SysMenuPrivilege::getMenuId, menuId));

        privilegeService.DeletePrivilege(menuId);

        //插入权限
        SysPrivilege privilege = new SysPrivilege();
        privilege.setPrivilegeId(menuId);
        privilege.setPrivilegeName("菜单权限");
        privilege.setType("MENU");
        privilege.setCreateTime(new Date());
        privilege.setCreateBy(userUtil.GetUserName());
        privilegeService.SavePrivilege(privilege);


        //插入关联
        SysMenuPrivilege menuPrivilege = new SysMenuPrivilege();
        menuPrivilege.setPrivilegeId(menuId);
        menuPrivilege.setMenuId(menuId);
        this.baseMapper.insert(menuPrivilege);


    }
}
